package jp.sinya;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 请求转发，请求包含，请求转发
 */
public class DemoServlet11 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("DemoServlet11-doGet");
        //请求转发 forward； 只可以设置请求头，请求体只能给下一个servlet处理
        resp.setStatus(200);
        resp.addHeader("Content-Type", "text/html; charset=utf-8");

        //请求转发，即便是转发的一方设置了响应体，也是没有效果的。会被处理转发的servlet覆盖
        resp.getWriter().write("DemoServlet11 封装的响应体...");
        req.getRequestDispatcher("/demo12").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求转发 include； 请求头和响应体 都可以处理
        resp.setStatus(200);

        resp.setHeader("Content-Type", "text/html; charset=utf-8");
        resp.getWriter().write("<p>DemoServlet11 封装的响应体...</p>");
        req.getRequestDispatcher("/demo12").include(req, resp);
    }
}
